import re


class EtlUtil(object):
    CLASS_NAME = ".etl.core.DriverApplication"
    COMPLEX_CLASS_NAME = ".etl.core.DriverApplicationComplex"
    # reader
    HIVE_READER_TYPE = ".etl.hive.HiveReader"
    JDBC_READER_TYPE = ".etl.jdbc.MySqlReader"
    KAFKA_READER_TYPE = ".etl.kafka.KafkaReader"
    # writer
    HIVE_WRITER_TYPE = ".etl.hive.HiveWriter"
    JDBC_WRITER_TYPE = ".etl.jdbc.MySqlWriter"
    KAFKA_WRITER_TYPE = ".etl.kafka.KafkaWriter"
    ES_WRITER_TYPE = ".etl.elasticsearch.ElasticsearchWriter"
    DORIS_WRITER_TYPE = ".etl.doris.DorisWriter"

    SOURCE_TYPE_KAFKA = 1
    SOURCE_TYPE_HIVE = 2
    SOURCE_TYPE_JDBC = 3
    SOURCE_TYPE_ES = 4
    SOURCE_TYPE_DORIS = 5

    DECODE_DEFAULT_TYPE = 1
    DECODE_DEFAULT_FILEDS = "value:string"
    DECODE_DEFAULT_DECODERCLASS = ".etl.decoder.DefaultValueDecoder"
    DECODE_DEFAULT_ES_FILEDS = "index_suffix:string,value:string"
    DECODE_DEFAULT_ES_DECODERCLASS = ".etl.decoder.DefaultEsDecoder"
    DECODE_DEFAULT_CUSTOM = 2

    SINK_TYPE_KAFKA = 1
    SINK_TYPE_HIVE = 2
    SINK_TYPE_JDBC = 3
    SINK_TYPE_ES = 4
    SINK_TYPE_DORIS = 5

    @staticmethod
    def sql_cluster_decoder(sql):
        sql = sql + " "
        result = {}
        regex_arr = ["(\\s+|\\*?)(from|FROM)\\s+(\\w+\\.)?(\\w+\\.)*(\\w+)(\\)*)\\s+(.|\\s)*?\\w*",
                     "(\\s+)(join|JOIN)\\s+(\\w+\\.)?(\\w+\\.)*(\\w+)\\s+(.|\\s)*?(on|ON)"]
        for regex in regex_arr:
            pattern = re.compile(regex)
            matcher = pattern.findall(sql)
            if matcher:
                for mat in matcher:
                    table = mat[4]
                    cluster = mat[2]
                    if cluster and cluster != '':
                        cluster = cluster[:len(cluster)-1]
                    if cluster not in result.keys():
                        tables = [table]
                        result[cluster] = tables
                    else:
                        tables = result[cluster]
                        tables.append(table)

        return result







